/**
 * @file     Rules.cpp
 * @brief    Implementation for the diferent rules.
 *
 * @author   Franco Riberi
 * @email    fgriberi AT gmail.com
 *
 * Contents: Header file for titere providing PredicateRule interface.
 *
 * System:   titere: Target Ideal TEmperature Rna Expression
 * Language: C++
 *
 * @date January 8, 2014
 *
 * Copyright (C) 2014 Franco Riberi, FuDePAN
 *
 * This file is part of the titere.
 *
 * Titere is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Titere is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with titere. If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <mili/mili.h>
#include "titere/WindowsRules.h"
#include "titere/TitereHelper.h"
#include "titere/Exceptions.h"

namespace NSTitere
{

/** @brief Represents important positions in a sequence */
static size_t FIRST_POSITION    = 0u;
static size_t SECOND_POSITION   = 1u;
static size_t OCTAVE_POSITION   = 7u;
static size_t NINETEEN_POSITION = 18u;
static size_t MINIMAL_EXPECTED  = 3u;
static size_t MAXIMAL_EXPECTED  = 8u;

//----------------------------------------------------------------------------------------------------------------
// StartWithAdenineOrUracil Rule
//----------------------------------------------------------------------------------------------------------------
bool StartWithAdenineOrUracil::checkRule(const biopp::NucSequence& sequence)
{
    const std::string seqToString = sequence.getString();
    const bool firstPosIsAdenineOrUracil = (seqToString[FIRST_POSITION] == ADENINE || seqToString[FIRST_POSITION] == URACIL);
    const bool secondPosIsAdenineOrUracil = (seqToString[SECOND_POSITION] == ADENINE || seqToString[SECOND_POSITION] == URACIL);
    return (firstPosIsAdenineOrUracil || secondPosIsAdenineOrUracil);
}

//----------------------------------------------------------------------------------------------------------------
// FirstEigthNucleotid
//----------------------------------------------------------------------------------------------------------------
bool FirstEigthNucleotide::checkRule(const biopp::NucSequence& sequence)
{
    const size_t adenineCount = NSHelperFuntion::countNucleotide(FIRST_POSITION, OCTAVE_POSITION, sequence, ADENINE);
    const size_t uracilCount = NSHelperFuntion::countNucleotide(FIRST_POSITION, OCTAVE_POSITION, sequence, URACIL);
    return ((adenineCount > MINIMAL_EXPECTED && adenineCount < MAXIMAL_EXPECTED) ||
            (uracilCount > MINIMAL_EXPECTED && uracilCount < MAXIMAL_EXPECTED));
}

//----------------------------------------------------------------------------------------------------------------
// NucleotidNineteen
//----------------------------------------------------------------------------------------------------------------
bool NucleotidNineteen::checkRule(const biopp::NucSequence& sequence)
{
    mili::assert_throw<InvalidPosition>(NINETEEN_POSITION < sequence.length());
    return (NSHelperFuntion::checkNucleotide(sequence, NINETEEN_POSITION, CYTOSINE) ||
            NSHelperFuntion::checkNucleotide(sequence, NINETEEN_POSITION, GUANINE));

}

} // namespace NSTitere
